﻿using Common.Contract.Authentication.Record;
using Common.Contract.CrossSelling.Record;
using Common.Contract.CrossSelling.Request;
using Common.Contract.CrossSelling.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrossSelling.Core.ServiceCommand
{
    public class RetrieveCustomerPreApprovedCreditCardForLoanServiceCommand : ProviderServiceCommand<IProviderFactory, RetrievePreApprovedCustCreditCardForLoanRequest, RetrievePreApprovedCustCreditCardForLoanResponse>
    {
        #region Properties
        public UserRecord Dao;
        public FilterRecord TextSearch;
        public int Type;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        #endregion
        public RetrieveCustomerPreApprovedCreditCardForLoanServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrievePreApprovedCustCreditCardForLoanRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            if (request.Type == 0)
            {
                throw new ArgumentException("Type must be set", "request");
            }
            else
            {
                Type = Request.Type;
            }
            TextSearch = Request.Filter;
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrievePreApprovedCustCreditCardForLoanResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<XSellingCreditCardForLoanRecord> records = new List<XSellingCreditCardForLoanRecord>();
                List<PreApprovedCreditCard21> listCustomer = new List<PreApprovedCreditCard21>();
                listCustomer = Providers.CrossSellingProducts.RetrieveListCustPreApprovedCreditCardForLoan(out totalRecord, Dao, Type, TextSearch, Request.Lang, pageSize, pageIndex);
                if (listCustomer != null)
                {
                    records = listCustomer.Select(x => new XSellingCreditCardForLoanRecord
                    {
                        Zone = x.Branchs.Region,
                        DaoName = string.IsNullOrEmpty(x.Cif) || x.Customers == null ? string.Empty : x.Customers.DaoName,
                        Dao = string.IsNullOrEmpty(x.Cif) || x.Customers == null ? string.Empty : x.Customers.Dao,
                        Branch = x.Branchs.BranchNameSme,
                        Cif = string.IsNullOrEmpty(x.Cif) ? string.Empty : x.Cif,
                        Name = string.IsNullOrEmpty(x.Cif) || x.Customers == null ? x.CusName : x.Customers.CusName,
                        VpbIndustry = string.IsNullOrEmpty(x.Cif) || x.Customers == null ? string.Empty : Request.Lang.Equals("vi") ? x.Customers.IndustryName : x.Customers.IndustryNameEn,
                        DateOpenCif = string.IsNullOrEmpty(x.Cif) || x.Customers == null ? DateTime.MinValue : x.Customers.CusOpenDate.HasValue ? x.Customers.CusOpenDate.Value : DateTime.MinValue,
                        GioiHanChoVayKhongTsbd = x.GioiHanChoVayKhongTsbd,
                        NgayPheDuyet = x.NgayPheDuyet,
                        CheckBctcThue = x.CheckBctcThue,
                        CheckConGioiHanChoVayKhongTsbd = x.CheckConGioiHanChoVayKhongTsbd,
                        CheckKhChuaCoThe = x.CheckKhChuaCoThe,
                        CheckKhoanVayPheDuyet = x.CheckKhoanVayPheDuyet,
                        CheckLhdn = x.CheckLhdn,
                        CheckThoiGianPheDuyet = x.CheckThoiGianPheDuyet
                    }).ToList();
                    response.Records = records;
                    response.Page = new PageRecord
                    {
                        PageIndex = pageIndex,
                        PageSize = pageSize,
                        TotalRecords = totalRecord
                    };
                }
                else
                {
                    isSuccess = false;
                    errorMessage = string.Format("Can not found customer with user={0}", Dao.UserName);
                }
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }
            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
